package com.isay.app.honey.ui.direct;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.isay.app.honey.R;
import com.isay.app.honey.ui.home.HomeItemInfo;
import com.isay.frameworklib.utils.ClickUtils;
import com.isay.frameworklib.utils.glide.GlideUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * 智批改recycler adapter
 * <p>
 * Created by cywu4 on 2017/3/13.
 */
public class DirectRecyclerAdapter extends RecyclerView.Adapter<DirectRecyclerAdapter.ViewHolder> implements View.OnClickListener {

    private Context mContext;
    private OnItemClick mListener;
    private ArrayList<DirectItemInfo> mData = new ArrayList<>();

    public interface OnItemClick {
        void onClick(int position);
    }

    public DirectRecyclerAdapter(Context context, OnItemClick l) {
        mContext = context;
        mListener = l;
    }


    public void addData(boolean refresh, List<DirectItemInfo> data) {
        if (refresh) {
            mData.clear();
        }
        if (data != null) {
            mData.addAll(data);
        }
    }


    @Override
    public int getItemCount() {
        return mData.size();
    }


    @Override
    public long getItemId(int i) {
        return 0;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_honey_direct, parent, false);
        ViewHolder holder = new ViewHolder(view);
        holder.layItem.setOnClickListener(this);
        return holder;
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        DirectItemInfo info = mData.get(position);
        holder.tvName.setText(info.getName());
        holder.tvCount.setText(info.getNum());
        if (info.getType() == 1) {
            //1解锁
            holder.mIvLock.setVisibility(View.GONE);
        } else {
            holder.mIvLock.setVisibility(View.VISIBLE);
        }
        holder.layItem.setTag(info.getType());
        GlideUtils.loadImageCircleCropWidthAnim(mContext, holder.ivImg, info.getLogo());
    }


    @Override
    public void onClick(View v) {
        if (ClickUtils.isFastClick()) {
            return;
        }
        Object tag = v.getTag();
        if (tag instanceof Integer) {
            if (mListener != null) {
                mListener.onClick((int) tag);
            }
        }
    }


    protected class ViewHolder extends RecyclerView.ViewHolder {

        private TextView tvName;
        private TextView tvCount;
        private View mIvLock;
        private ImageView ivImg;
        private View layItem;

        public ViewHolder(View itemView) {
            super(itemView);
            layItem = itemView.findViewById(R.id.lay_home_item);
            ivImg = itemView.findViewById(R.id.iv_img);
            tvName = itemView.findViewById(R.id.tv_name);
            tvCount = itemView.findViewById(R.id.tv_count);
            mIvLock = itemView.findViewById(R.id.iv_lock);
        }
    }

}
